Validation = function(options) {
		this.fields = [];
		this.errors = [];
		this.addAfter = null;
		this.timeOutId = null;

		this.fields = options.fields;
		this.addAfter = options.addAfter;
		this.afterValidation = options.afterVal ? options.afterVal : null;

		
		this.fullCheck = function() {
			for(var i in this.fields) {
				this.fieldCheck(this.fields[i]);
			}
			
			if(this.errors.length) {
				this.showErrors();
				return false;
			}
			else if(this.afterValidation) {
				this.afterValidation(this);
				if(this.errors.length) {
					this.showErrors();
					return false;
				}
				else return true;
			}
			else return true;

		};
		
		this.fieldCheck = function(fieldArray) {
			if(fieldArray.rules.mail) {
				this.mailValidation(fieldArray.fieldId);
				return;
			}
			if(fieldArray.rules.pass) {
				this.passValidation(fieldArray.fieldId, fieldArray.confId);
				return;
			}
			if($('#' + fieldArray.fieldId).val() == '' && fieldArray.rules.allowNull) return true;
			if(trim($('#' + fieldArray.fieldId).val()) == '' && !fieldArray.rules.allowNull) {
				 this.setError(fieldArray.rules.errNullText ? fieldArray.rules.errNullText : 'Заполните поле');
				return false;
			}
			
			if(fieldArray.rules.regExp) this.regExpValidation(fieldArray.fieldId, fieldArray.rules.regExp);
			if(fieldArray.rules.select) this.selectValidation(fieldArray.fieldId, fieldArray.rules.select);
		};
		
		this.regExpValidation = function(fieldId, regExp) {
			if(!(regExp.val).test($('#' + fieldId).val())) this.setError(regExp.errText);
		};
		
		this.selectValidation = function(fieldId, select) {
			if(!$('#' + fieldId + ' option:selected').val() || $('#' + fieldId + ' option:selected').val() == 0) this.setError(select.errText);
		};
		
		this.mailValidation = function(fieldId) {
			var mailExp = /^[a-z0-9_\.\-]{1,20}@[a-z0-9\.\-]{1,20}\.[a-z]{2,4}$/i;
			if($('#' + fieldId).val() == '')  this.setError('Укажите e-mail');
			else if(!mailExp.test($('#' + fieldId).val()))  this.setError('Неверно заполнен e-mail');
		};
		
		this.passValidation = function(fieldId, confId) {
			var passExp = /^[\S]{6,64}$/i;
			if($('#' + fieldId).val() == '')  this.setError('Укажите пароль');
			else if(!passExp.test($('#' + fieldId).val()))  this.setError('Пароль - от 6 до 64 символов');
			else if($('#' + fieldId).val() != $('#' + confId).val())  this.setError('Пароль и подтверждение не совпадают');
		};
		
		this.setError = function(text) {
			this.errors.push(text);
		};
		
		this.showErrors = function() {
			var string = '';
			for(var i in this.errors) {
				string += '<div class="err-item">' + this.errors[i] + '</div>';
			}
			
			if(!$('.jerrors', this.addAfter).length) $errDiv = this.addAfter.prepend('<div class="jerrors val-err"></div>');
			$('.jerrors', this.addAfter).html(string);
			if(!$('.jerrors', this.addAfter).is(":visible")) {
				$('.jerrors', this.addAfter).clearQueue().slideDown();
			}
			else clearTimeout(this.timeOutId);
			
			this.timeOutId = setTimeout(function() {$('.jerrors', this.addAfter).slideUp();}, 2500);
			this.errors = [];
		};
		
		this.clearErrors = function() {
			$('.jerrors', this.addAfter).hide();
			clearTimeout(this.timeOutId);
		};
}